package com.ants.modules.quartz.job;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * TODO
 * Author Chen
 * Date   2023/12/7 14:56
 */
public class DeleteFileJob implements Job {


    @Value(value = "${ants.path.upload}")
    private String uploadpath;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<File> files = FileUtil.loopFiles(uploadpath + File.separator + "fileTools");
        for (File file : files) {
            Date date = FileUtil.lastModifiedTime(file);
            long between = DateUtil.between(new Date(), date, DateUnit.MINUTE);
            if (between > 5) {
                // 删除5分钟前的文件
                FileUtil.del(file);
            }
        }
    }

}
